package nl.windgazer.arachne.components.socialmedia.facebook;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import nl.windgazer.arachne.services.facebook.FacebookShareLinkType;

import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

/**
 * This component attempts to implement most of the functionality available for the
 * http://developers.facebook.com/docs/reference/plugins/like, when not supporting
 * the JavaScript SDK on the page. For this functionallity to work you MUST also use
 * the @IncludeFaceBookOpenGraphInfo on the page for which this component is used, as
 * it is quite possible to provide a like button linked to a different page the
 * annotation is NOT enforced by this component.<br />
 * The only thing currently not supported is alternate font-faces.<br />
 * @author mreuring
 *
 */
public class FacebookLikeLink {
	
	public enum Layout {
		standard, button_count
	}
	
	public enum ColorScheme {
		light, dark
	}
	
	public enum Action {
		like, recommend
	}

	//Injects and Persists

	@Environmental
	private JavaScriptSupport _js;

	//Properties and Parameters

	/**
	 * The full url to the page you want to 'like'. When showing a product-overview on your
	 * page this should be a perma-link to the actual product details page. 
	 */
	@Parameter(required=true)
	private String url;

	/**
	 * This property is to determine wether or not to show 'faces' with the link. When enabled
	 * this will show a limited number of friends who recently 'like'-ed this link.
	 */
	@Parameter
	@Property
	private boolean faces = false;

	/**
	 * The width of the iframe
	 */
	@Parameter
	@Property
	private int width = 450;

	/**
	 * The layout of the button, default is the standard with elaborate text and faces.
	 * The alternative is a simplistic view showing only the button and number of likes.
	 */
	@Parameter
	@Property
	private Layout layout = Layout.standard;

	/**
	 * The default color-scheme is 'light', the alternate is 'dark'.<br />
	 * See http://developers.facebook.com/docs/reference/plugins/like for more details.
	 */
	@Parameter
	private ColorScheme colorscheme = ColorScheme.light;

	/**
	 * The default action is of course 'like' but there is the alternative 'recommend' option.
	 */
	@Parameter
	private Action action = Action.like;

	/**
	 * This value is pre-calculated based on 'layout' and 'faces'.
	 */
	@Property(write=false)
	private int height = 80;

	@SuppressWarnings("unused")
	@Parameter
	@Property
	private FacebookShareLinkType type = FacebookShareLinkType.button_count;

	private StringBuilder query;

	//Components

	//Events

	@SetupRender
	void setupMetaTags() throws UnsupportedEncodingException {
		
		//Create new StringBuilder (for thread-safety, do it here)
		query = new StringBuilder();

		// Add link to FB-share script @ http://static.ak.fbcdn.net/connect.php/js/FB.Share
		_js.importJavaScriptLibrary("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
		setHeight();
		buildQuery();

	}

	//Other (check for business-logic, it should not remain here)

	public void setHeight() {
		if (layout.equals(Layout.button_count)) {
			height = 21;
		} else if (!faces) {
			height = 35;
		} else {
			height = 80;
		}
	}
	
	/**
	 * <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe>
	 * <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:21px;" allowTransparency="true"></iframe>
	 * <iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fpage%2Fto%2Flike&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=recommend&amp;font=tahoma&amp;colorscheme=dark&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>
	 */
	private void buildQuery() throws UnsupportedEncodingException {
		if (url!=null) {
			addQuery("href", url);
		}
		if (!layout.equals(Layout.standard)) {
			addQuery("layout", layout.toString());
		}
		if(!faces) {
			addQuery("show_faces", String.valueOf(faces));
		}
		addQuery("width", String.valueOf(width));
		if (!action.equals(Action.like)) {
			addQuery("action", action.toString());
		}
		if (!colorscheme.equals(ColorScheme.light)) {
			addQuery("colorscheme", colorscheme.toString());
		}
		addQuery("height", String.valueOf(height));
	}

	private void addQuery(String key, String value) throws UnsupportedEncodingException {
		if (query.length() == 0) {
			query.append('?');
		} else {
			query.append('&');
		}
		query.append(URLEncoder.encode(key, "UTF-8"));
		query.append('=');
		query.append(URLEncoder.encode(value, "UTF-8"));
	}

	public String getQuery() {
		return query.toString();
	}
}
